package com.pickup.controller;

import com.pickup.aspect.WebLog;
import com.pickup.enums.OperatorFriendRequestTypeEnum;
import com.pickup.pojo.Users;
import com.pickup.req.bo.UsersBO;
import com.pickup.req.vo.FriendApplicationVo;
import com.pickup.req.vo.MyFriendsVO;
import com.pickup.req.vo.SmsCodeVo;
import com.pickup.resp.JSONResult;
import com.pickup.service.UserService;
import com.pickup.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("u")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserUtil userUtil;



    // 用户登录
    @PostMapping("/userLogin")
    @WebLog(description = "用户登录!")
    public JSONResult userLogin(@RequestBody Users user) throws Exception {

        if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
            return JSONResult.build(500, "用户名或密码不能为空！", null);
        }
        return userService.queryUserForLogin(user.getUsername(), MD5Utils.getMD5Str(user.getPassword()));
    }


    // 用户注册 --- 发送验证码
    @PostMapping("/UserPhoneRegister")
    public JSONResult UserPhoneRegister(@RequestBody Users user) throws Exception {
        // 0. 判断用户名和密码不能为空
        if (StringUtils.isBlank(user.getNickname()) || StringUtils.isBlank(user.getCellphone()) || StringUtils.isBlank(user.getPassword())) {
            return JSONResult.build(500, "用户昵称/密码/手机号不能为空!", null);
        }
        return userService.queryCellPhoneIsExist(user);
    }

    // 用户注册--->验证验证码是否正确
    @PostMapping("/verificationCode")
    public JSONResult verificationCode(@RequestBody SmsCodeVo smsCodeVo) throws Exception {
        // 0. 判断用户名和密码不能为空
        if (StringUtils.isBlank(smsCodeVo.getSmsCode())) {
            return JSONResult.build(500, "验证码不能为空!", null);
        }
        return userService.saveUser(smsCodeVo);
    }

    // 根据手机号码找回密码---->发送验证码
    @PostMapping("/getVerification")
    public JSONResult getVerification(@RequestBody Users user) throws Exception {
        // 0. 判断用户名和密码不能为空
        if (StringUtils.isBlank(user.getCellphone())) {
            return JSONResult.build(500, "手机号不能为空", null);
        }
        return userService.getCode(user);
    }

    // 根据手机号码找回密码---->判断验证码是否正确
    @PostMapping("/retrievePass")
    public JSONResult retrievePass(@RequestBody SmsCodeVo smsCodeVo) throws Exception {
        System.out.println("2");
        // 0. 判断用户名和密码不能为空
        if (StringUtils.isBlank(smsCodeVo.getCellphone()) || StringUtils.isBlank(smsCodeVo.getSmsCode())) {
            return JSONResult.build(500, "手机号/验证码不能为空！", null);
        }
        return userService.bindMobile(smsCodeVo);
    }

    // 重制密码
    @PostMapping("/resetPassword")
    public JSONResult resetPassword(@RequestBody Users users) throws Exception {
        // 0. 判断用户名和密码不能为空
        if (StringUtils.isBlank(users.getCellphone()) || StringUtils.isBlank(users.getPassword())) {
            return JSONResult.build(500, "手机号/密码不能为空！", null);
        }
        return userService.resetPassword(users);

    }


    // 上传用户头像
    @PostMapping("/uploadFaceBase64")
    public JSONResult uploadFaceBase64(@RequestBody UsersBO userBO) throws Exception {

        if (userBO.getFaceData() == null) {
            return JSONResult.build(500, "设置失败", null);
        }
        return userService.updateUserInfo(userBO);
    }

    // 设置用户昵称
    @PostMapping("/setNickname")
    public JSONResult setNickname(@RequestBody UsersBO userBO) throws Exception {
        System.out.println(userBO.getUserId() + "   " + userBO.getNickname());
        if (userBO.getUserId() == null || userBO.getNickname() == null) {
            return JSONResult.build(500, "设置失败", null);
        }
        return userService.updateUserNickname(userBO);
    }

    // 搜索好友接口, 根据账号做匹配查询而不是模糊查询
    @PostMapping("/searchFriend")
    public JSONResult searchUser(@RequestBody FriendApplicationVo friendApplicationVo) throws Exception {
        // 0. 判断 myUserId friendUsername 不能为空
        if (StringUtils.isBlank(friendApplicationVo.getFriendKeyword())) {
            return JSONResult.build(500, "请输入添加用户名称！", null);
        }
        return userService.preconditionSearchFriends(friendApplicationVo.getSelfId(), friendApplicationVo.getFriendKeyword());
    }

    // 发送添加好友的请求
    @PostMapping("/addFriendRequest")
    public JSONResult addFriendRequest(String myUserId, String friendUsername) throws Exception {
        // 0. 判断 myUserId friendUsername 不能为空
        if (StringUtils.isBlank(myUserId) || StringUtils.isBlank(friendUsername)) {
            return JSONResult.build(500, "输入不能为空！", null);
        }
        userService.sendFriendRequest(myUserId, friendUsername);
        return JSONResult.build(200, "添加信息已发送！", null);
    }

    // 查询好友添加的请求
    @PostMapping("/queryFriendRequests")
    public JSONResult queryFriendRequests(String userId) {
        // 0. 判断不能为空
        if (StringUtils.isBlank(userId)) {
            return JSONResult.build(500, "添加失败！", null);
        }
        // 1. 查询用户接受到的朋友申请
        return JSONResult.build(200, "添加成功！", userService.queryFriendRequestList(userId));
    }

    // 接受方 通过或者忽略朋友请求
    @PostMapping("/operFriendRequest")
    public JSONResult operFriendRequest(String acceptUserId, String sendUserId, Integer operType) {
        // 0. acceptUserId sendUserId operType 判断不能为空
        if (StringUtils.isBlank(acceptUserId) || StringUtils.isBlank(sendUserId) || operType == null) {
            return JSONResult.build(500, "添加失败", null);
        }
        // 1. 如果operType 没有对应的枚举值，则直接抛出空错误信息
        if (StringUtils.isBlank(OperatorFriendRequestTypeEnum.getMsgByType(operType))) {
            return JSONResult.build(500, "添加失败", null);
        }
        if (operType.equals(OperatorFriendRequestTypeEnum.IGNORE.type)) {
            // 2. 判断如果忽略好友请求，则直接删除好友请求的数据库表记录
            userService.deleteFriendRequest(sendUserId, acceptUserId);
        } else if (operType.equals(OperatorFriendRequestTypeEnum.PASS.type)) {
            // 3. 判断如果是通过好友请求，则互相增加好友记录到数据库对应的表
            //	  然后删除好友请求的数据库表记录
            userService.passFriendRequest(sendUserId, acceptUserId);
        }
        // 4. 数据库查询好友列表
        List<MyFriendsVO> myFirends = userService.queryMyFriends(acceptUserId);
        return JSONResult.build(200, "添加成功", myFirends);
    }

    // 查询我的好友列表
    @PostMapping("/myFriends")
    public JSONResult myFriends(String userId) {
        // 0. userId 判断不能为空
        if (StringUtils.isBlank(userId)) {
            return JSONResult.build(500, "查询失败", null);
        }
        // 1. 数据库查询好友列表
        List<MyFriendsVO> myFirends = userService.queryMyFriends(userId);
        return JSONResult.build(200, "查询成功", myFirends);
    }


}
